
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
/**
 * 可关闭的JTabbedPane标签
 *
 * @author monitor
 * Created on 2011-2-25, 23:52:41
 */
public class ClosedPanelTeset extends JPanel {
    private final JTabbedPane pane;
    public ClosedPanelTeset(final JTabbedPane pane){
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if(pane==null) throw new NullPointerException("TabbedPane is null");
        this.pane=pane;
        setOpaque(false);
        //tab标题
        JLabel label = new JLabel() {
            @Override
            public String getText() {
                int i = pane.indexOfTabComponent(ClosedPanelTeset.this);
                if (i != -1)return pane.getTitleAt(i);
                return null;
            }
        };
        add(label);
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
        add(new TabButton());
        setBorder(BorderFactory.createEmptyBorder(1, 0, 0, 0));
    }
    /** tab上的关闭按钮 */
    private class TabButton extends JButton {
        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("关闭");
            setUI(new BasicButtonUI());
            setContentAreaFilled(false);
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //翻转效果
            setRolloverEnabled(true);
            //鼠标事件，进入时画边框，移出时取消边框
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    Component component = e.getComponent();
                    if (component instanceof AbstractButton) {
                        AbstractButton button = (AbstractButton) component;
                        button.setBorderPainted(true);
                    }
                }
                @Override
                public void mouseExited(MouseEvent e) {
                    Component component = e.getComponent();
                    if (component instanceof AbstractButton) {
                        AbstractButton button = (AbstractButton) component;
                        button.setBorderPainted(false);
                    }
                }
            });
            //单击关闭按钮事件
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = pane.indexOfTabComponent(ClosedPanelTeset.this);
                    if (i != -1)  pane.remove(i);
                }
            });
        }
        @Override
        public void updateUI() {
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //鼠标按下时偏移一个坐标点
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            //鼠标在按钮上时为红色
            if (getModel().isRollover()) {
                g2.setColor(Color.RED);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new JFrame().add(new ClosedPanelTeset(new JTabbedPane())).setVisible(true));
    }
}
